package com.atguigu.controller;

import com.atguigu.pojo.Emp;
import com.atguigu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: liuaojun
 * @CreateTime: 2025-02-05
 * @Description:
 * @Version: 1.0
 */
@RestController
//@RestController = @Controller + @ResponseBody
//@ResponseBody响应数据
//响应数据时必须在方法上(类上)添加@ResponseBody
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("/emp")
    String insertEmp(@RequestBody Emp emp) {//@RequestBody:处理请求数据(Json)
        userService.insertUser(emp);
        return "{'status':'ok'}";
    }

    @PutMapping("/emp")
    String updateEmp(@RequestBody Emp emp) {
        userService.updateUser(emp);
        return "{'status':'ok'}";
    }

    @DeleteMapping("/emp/{eid}")
    String deleteEmp(@PathVariable("eid") int id) {
        userService.deleteUser(id);
        return "{'status':'ok'}";
    }

    @GetMapping("/emp/{eid}")
    Emp getEmpById(@PathVariable("eid") Integer id) {
        Emp empById = userService.getEmpById(id);
        return empById;
    }

    @GetMapping("/emp")
    List<Emp> getAllEmp() {
        List<Emp> allEmp = userService.getAllEmp();
        return allEmp;
    }



}
